package com.wmx.datastructuresandalgorithms.hashtable;

import java.util.Scanner;

import static java.lang.System.in;

/**********************************
 * @author wmx
 * @date 2019/9/5 10:37
 * @description:
 * @version 1.0
 ************************************/
public class HashTableDemo {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String key = "";

        MyHashTable<Student> objectMyHashTable = new MyHashTable<>(7);

        while (true){
            System.out.println("添加 add");
            System.out.println("显示 list");
            System.out.println("退出 exit");
           key = scanner.next();
           switch(key){
               case "add":
                   System.out.println("请输入id");
                   int id = scanner.nextInt();
                   System.out.println("请输入name");
                   String name = scanner.next();
                   objectMyHashTable.add(new Student(id ,name) );
                   break;
               case "list":
                   objectMyHashTable.disPlay();
                   break;
               case "exit":
                   System.out.println("退出");
                   scanner.close();
                   System.exit(0);
                   break;
               default:
                   break;
           }

        }

    }

    static class Student{
        private int id;
        private String name ;
        Student(int id ,String name){
            this.id = id ;
            this.name = name ;
        }
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    static class MyHashTable<T>{
        private int size ;
        private SingleLinkedList<T> [] singleLinkedLists ;
        MyHashTable(int size){
             this.size = size ;
             this.singleLinkedLists = new SingleLinkedList[size];
            for (int i = 0; i <this.singleLinkedLists.length; i++) {
                this.singleLinkedLists[i] = new SingleLinkedList<T>();
            }
        }

        private void add(T t){
            int code = myHashCode(t);
            for (int i = 0; i <singleLinkedLists.length ; i++) {
                if(i == code){
                    singleLinkedLists[i].add(t);
                    return;
                }
            }
        }


        private void disPlay(){

            for (int i = 0; i <singleLinkedLists.length ; i++) {
                    singleLinkedLists[i].disPlay(i);
            }
        }

        private int myHashCode(T t){
            Student student = (Student) t;
            return  (student.getId() % size);
        }


    }
    static class SingleLinkedList<T>{
        private Node head ;

        class Node{
            private Node next ;
            private T data;
            Node(T data){
                this.data  = data ;
            }
        }

        //添加元素
        private void add(T data){
            Node  node = new Node(data);
            if(head == null){
                head = node ;
                return;
            }
            Node cure = head ;
            while (true){
               if(cure.next == null){
                  break;
               }
            }

            cure.next = node;

        };

        public void disPlay(int i){
            if(head == null){
                System.out.println("第"+i +"哈希表中还没有数据，请添加数据");
                return;
            }
            Node cure = head;
            System.out.printf("第"+i +"哈希表中：");
            System.out.printf("[");
            while (true){
                if(cure.next == null){
                    System.out.printf(cure.data.toString()+"]");
                    break;
                }else {
                    System.out.printf(cure.data.toString()+"—>");
                }
                cure = cure.next;
            }
            System.out.println();
        }
    }

}
